package com.im.common.interceptor;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.im.common.config.IgnoreUrlsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * Sa-Token拦截器
 *
 * @author yunZhouJiaDi
 */
@Slf4j
@Configuration
public class SaTokenInterceptor implements WebMvcConfigurer {

    @Resource
    private IgnoreUrlsConfig ignoreUrlsConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> ignoreUrls = ignoreUrlsConfig.getUrls();
        registry.addInterceptor(new SaInterceptor(handle -> {
            StpUtil.checkLogin();
            StpUtil.renewTimeout(172800);
        })).addPathPatterns("/**").excludePathPatterns(ignoreUrls);
    }

}
